import 'package:flutter_yn_goods/config/app_config.dart';
import 'package:flutter_yn_goods/config/app_translations.dart';
import 'package:flutter_yn_goods/user/app_user_manager.dart';
import 'package:flutter_yn_goods/utils/http/http_export.dart';
import 'package:flutter_yn_goods/utils/loading/loading_utils.dart';
import 'package:flutter_yn_goods/widgets/common_normal_dialog.dart';
import 'package:get/get.dart';
import 'package:url_launcher/url_launcher.dart';

class SettingsLogic extends GetxController {
  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  void loginOut() async {
    var toOutLogin = await CommonNormalDialog(
      title: LocaleKeys.logout.tr,
      message: LocaleKeys.logout_confirm.tr,
      barrierDismissible: false,
    ).show();
    if (true == toOutLogin) {
      AppUserManager().logout();
      Get.back();
    }
  }

  void cancelAccount() async {
    var toCancel = await CommonNormalDialog(
      title: LocaleKeys.cancel_account.tr,
      message: LocaleKeys.cancel_account_confirm.tr,
      barrierDismissible: false,
      onSure: () async {
        var response = await HttpManager()
            .get(API.cancelAccount, uiType: HttpUIType.loading);

        if (response.isSuccess) {
          LoadingUtils.showToast(LocaleKeys.cancel_account_success.tr);
          AppUserManager().logout();
        } else {
          LoadingUtils.showToast(
              '${LocaleKeys.cancel_account_failed.tr}${response.message}');
        }
      },
    ).show();
  }

  openEmailApp() async {
    final Uri emailLaunchUri = Uri(
      scheme: 'mailto',
      path: AppConfig.appConfig?.kefuMail,
    );

    await launchUrl(emailLaunchUri);
  }
} 